package com.rear.inter.base;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.rear.utils.StringUtil;

public class BaseAction {
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * 远程jsop请求基础服务
	 * 	将处理好的json数据进行封装返回
	 * @param jsonDate 要返回客户端的json数据
	 */
	protected String ajaxJsonResponse(HttpServletResponse response, HttpServletRequest request,String jsonDate){
		
		try {
		 
        String callback = request.getParameter("callback");  
        System.out.println("***********************callback************"+callback);
        String returnResult = "";  
        if(StringUtil.isNotEmpty(callback)){  
        	returnResult = callback  + "("+jsonDate+")";  
       }else{
    	   returnResult = jsonDate;
       }  
        response.setContentType("text/json;charset=utf-8");
        response.addHeader("Access-Control-Allow-Origin","*");
        
        response.getWriter().write(returnResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return null;
        
	}
	protected String  getClientIp(HttpServletRequest request){
			
			String address = request.getHeader("X-Forwarded-For");
			if (address != null && address.length() > 0
					&& !"unknown".equalsIgnoreCase(address)) { 
				return address;
			}
			address = request.getHeader("Proxy-Client-IP");
			if (address != null && address.length() > 0
					&& !"unknown".equalsIgnoreCase(address)) {
				return address;
			}
			address = request.getHeader("WL-Proxy-Client-IP");
			if (address != null && address.length() > 0
					&& !"unknown".equalsIgnoreCase(address)) {
				return address;
			}
			
			address = request.getRemoteAddr();
			if (address != null && address.length() > 0
					&& !"unknown".equalsIgnoreCase(address)) {
				return address;
			}
			
			return address;
		}
}
